package cn.tedu.suppermemory.service.impl;

import cn.tedu.suppermemory.ex.ServiceException;
import cn.tedu.suppermemory.mapper.SubjectsMapper;
import cn.tedu.suppermemory.pojo.dto.SubjectsAddDTO;
import cn.tedu.suppermemory.pojo.dto.SubjectsUpdateDTO;
import cn.tedu.suppermemory.pojo.entity.Subjects;
import cn.tedu.suppermemory.pojo.vo.SubjectsListItemVO;
import cn.tedu.suppermemory.pojo.vo.SubjectsStandardVO;
import cn.tedu.suppermemory.service.ISubjectsService;
import cn.tedu.suppermemory.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SujectsServiceImpl implements ISubjectsService {
    @Autowired
    private SubjectsMapper subjectsMapper;
    @Override
    public void addNew(SubjectsAddDTO subjectsAddDTO) {
        log.debug("开始处理【添加课程】的业务，参数：{}",subjectsAddDTO);
        String name=subjectsAddDTO.getName();
        int countByname=subjectsMapper.countByName(name);
        if(countByname>0){
            String message = "添加课程失败，课程名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Subjects subject=new Subjects();
        BeanUtils.copyProperties(subjectsAddDTO,subject);
        int rows=subjectsMapper.insert(subject);
        if (rows != 1) {
            String message = "添加课程失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除课程】的业务，参数：{}", id);
        SubjectsStandardVO standardVO=subjectsMapper.getStandardById(id);
        if(standardVO==null){
            String message = "删除课程失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将执行删除，参数：{}", id);
        int rows=subjectsMapper.deleteById(id);
        if(rows!=1){
            String message = "删除课程失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoSubjects(Long id, SubjectsUpdateDTO subjectsUpdateDTO) {
        log.debug("开始处理【修改课程】的业务，参数：{}", subjectsUpdateDTO);
        Subjects subject=new Subjects();
        BeanUtils.copyProperties(subjectsUpdateDTO,subject);
        subject.setId(id);
        int rows=subjectsMapper.update(subject);
        if(rows!=1){
            String message = "修改课程详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public List<SubjectsListItemVO> list() {
        log.debug("开始处理【查询课程列表】的业务，无参数");
        List<SubjectsListItemVO> list= subjectsMapper.list();
        return list;
    }

    @Override
    public SubjectsStandardVO getStandradById(Long id) {
        log.debug("开始处理【根据id查询课程详情】的业务，参数：{}", id);
        SubjectsStandardVO standardVO=subjectsMapper.getStandardById(id);
        if(standardVO==null){
            String message = "查询课程详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回结果：{}",standardVO);
        return standardVO;
    }
}
